#pragma once

#include <QTextEdit>
#include <QByteArray>
#include <QScrollBar>
#include <QScroller>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QTimer>
#include <QLabel>
#include <QUrl>
#include <QPixmap>
#include <QString>
#include <QTemporaryFile>
#include <QDir>
#include "conn.hpp"
#include "log.hpp"
#include "cmdvar.hpp"

namespace cloud
{

class ResDialog : public QDialog
{
    Q_OBJECT
public:
    ResDialog(const QString& uri, QWidget *parent = nullptr) : QDialog(parent)
    {
        HttpConn conn;
        QUrl url = conn._svr_addr + uri;
        QByteArray content = conn.request(url);

        QWidget* widget = nullptr;

        if (uri.endsWith(".png") || uri.endsWith(".jpg"))
        {
            QLabel *imageLabel = new QLabel(this);
            QPixmap pmap;
            pmap.loadFromData(content);
            imageLabel->setPixmap(pmap);
            widget =imageLabel;
        }
        else if (uri.endsWith(".mp4"))
        {

            QTemporaryFile temp("xxxxxxx.mp4");
            if (temp.open())
            {
                temp.setAutoRemove(false);
                temp.write(content);
                temp.flush();
                temp.close();
            }

            QMediaPlayer *player = new QMediaPlayer(this);
            QVideoWidget *videoWidget = new QVideoWidget(this);
            player->setVideoOutput(videoWidget);
            player->setMedia(QUrl::fromLocalFile(temp.fileName()));
            player->play();
            videoWidget->show();
            widget = videoWidget;
            temp.setAutoRemove(true);
        }
        else
        {
            QTextEdit *textEdit = new QTextEdit(this);
            textEdit->setText(content);
            textEdit->setReadOnly(true);
            textEdit->setTextInteractionFlags(Qt::NoTextInteraction);
            //textEdit->verticalScrollBar()->setHidden(true);
            //textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

            QScroller* scroller = QScroller::scroller(textEdit->viewport());
            if (QSysInfo::productType() == "embedded")
                scroller->grabGesture(textEdit->viewport(), QScroller::TouchGesture);
            else
                scroller->grabGesture(textEdit->viewport(), QScroller::LeftMouseButtonGesture);

            widget = textEdit;
        }

        QPushButton *closeButton = new QPushButton(tr("Close"), this);
        connect(closeButton, &QPushButton::clicked, this, &ResDialog::close);

        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(widget);
        layout->addWidget(closeButton);
        setLayout(layout);

        setResDialog();
    }

    void setResDialog()
    {
        double scale = CmdHelper::instance()->value("r").toDouble();
        if (scale == 0.0)
            scale = 0.5; // if scale is equal with 0, then those quantities will be overflowed

        scale = 1 / scale; // [1, +infinity) -> (0, 1]

        QDialog::setGeometry(400 - 400/scale, 240 - 240/scale, 800/scale, 480/scale);

        DEBUG("text box attributes set,  left: " + QString::number(400 - 400/scale) +
                                        " low: " + QString::number(240 - 240/scale) +
                                       " long: " + QString::number(800/scale) +
                                     " height: " + QString::number(480/scale));
    }
};

}
